package com.sbx;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sbx.constants.UserConstant;
import com.sbx.entity.dto.AppStore;
import com.sbx.entity.dto.User;
import com.sbx.entity.dto.UserApp;
import com.sbx.mapper.AppStoreMapper;
import com.sbx.mapper.UserAppMapper;
import com.sbx.mapper.UserMapper;
import com.sbx.service.UserService;
import com.sbx.utils.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.Date;
import static com.sbx.constants.CommonConstant.InitId.*;
import static com.sbx.constants.CommonConstant.minio.RES_BUCKET;
import static com.sbx.constants.CommonConstant.minio.USERS_BUCKET;

@SpringBootApplication
@Slf4j
public class SibuxiangApplication {

    public static void main(String[] args) {
        SpringApplication.run(SibuxiangApplication.class, args);
    }

    @Resource
    private AppStoreMapper appStoreMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserAppMapper userAppMapper;

    @Resource
    private MinioUtil minioUtil;

    @Bean
    public CommandLineRunner initialization() {
        return new CommandLineRunner() {
            @Override
            public void run(String... args) throws Exception {

                //创建app
                if (appStoreMapper.selectById(APPSTORE_USER_MANAGER_ID) == null) {
                    AppStore userManagerStore = new AppStore();
                    userManagerStore.setId(APPSTORE_USER_MANAGER_ID)
                            .setName("用户管理")
                            .setIcon("el-icon-s-custom")
                            .setNote("管理用户")
                            .setCreated(new Date())
                            .setPath("/userManager");
                    appStoreMapper.insert(userManagerStore);
                }

                if (appStoreMapper.selectById(APPSTORE_APP_MANAGER_ID) == null) {
                    AppStore appManagerStore = new AppStore();
                    appManagerStore.setId(APPSTORE_APP_MANAGER_ID)
                            .setName("app管理")
                            .setIcon("el-icon-s-shop")
                            .setNote("管理用户app")
                            .setCreated(new Date())
                            .setPath("/appManager");
                    appStoreMapper.insert(appManagerStore);
                }


                // 创建用户
                if (userMapper.selectById(ADMIN_ID) == null) {
                    User user = new User();
                    user.setRoleId(UserConstant.UserRoleENUM.MANAGER.getRoleId())
                            .setNickName("admin")
                            .setUserName("admin")
                            .setState(UserConstant.UserStateENUM.NORMAL.getCode())
                            .setPassword("admin")
                            .setEmail("123@qq.com")
                            .setId(ADMIN_ID);
                }
                if (userMapper.selectById(COMMON_USER_ID) == null) {
                    User user = new User();
                    user.setRoleId(UserConstant.UserRoleENUM.MANAGER.getRoleId())
                            .setNickName("chenxiaodun")
                            .setUserName("xxx_chenxiaodun")
                            .setState(UserConstant.UserStateENUM.NORMAL.getCode())
                            .setPassword("admin")
                            .setEmail("123@qq.com")
                            .setId(COMMON_USER_ID);
                }

                LambdaQueryWrapper<UserApp> userAppLambdaQueryWrapper = new LambdaQueryWrapper<>();
                userAppLambdaQueryWrapper.eq(UserApp::getUserId, ADMIN_ID);
                if (CollectionUtils.isEmpty(userAppMapper.selectList(userAppLambdaQueryWrapper))) {
                    UserApp userApp = new UserApp();
                    userApp.setAppId(APPSTORE_APP_MANAGER_ID)
                            .setUserId(ADMIN_ID)
                            .setCreated(new Date());
                    userAppMapper.insert(userApp);
                    UserApp userApp1 = new UserApp();
                    userApp1.setAppId(APPSTORE_USER_MANAGER_ID)
                            .setUserId(ADMIN_ID)
                            .setCreated(new Date());
                    userAppMapper.insert(userApp1);
                }


                // 创建桶
                try {
                    if (!minioUtil.existBucket(USERS_BUCKET)) {
                        minioUtil.createBucket(USERS_BUCKET);
                    }
                    if (!minioUtil.existBucket(RES_BUCKET)) {
                        minioUtil.createBucket(RES_BUCKET);
                    }

                } catch (Exception e) {
                    log.error("桶创建失败");
                }


            }
        };
    }


}
